package com.rex.saas.service;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.rex.saas.bean.*;
import com.rex.saas.bean.request.SpaceEnergyRequest;
import com.rex.saas.bean.request.SpaceInComeRequest;
import com.rex.saas.bean.response.*;
import com.rex.saas.constants.Constants;
import com.rex.saas.db.entity.*;
import com.rex.saas.db.mapper.*;
import com.rex.saas.enums.SpaceTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName DeviceService
 * @Description:
 * @Author: zhusiyu
 * @CreateDate: 2024/12/18 11:06	//创建时间
 */
@Component
@Slf4j
public class StatisticsService {

    @Autowired
    private RexolarApiClient rexolarApiClient;

    @Autowired
    private SaasSpaceDeviceRelationMapper saasSpaceDeviceRelationMapper;

    @Autowired
    private SaasDeviceStatisticsMapper saasDeviceStatisticsMapper;

    @Autowired
    private SaasSpaceStatisticsMapper saasSpaceStatisticsMapper;

    @Autowired
    private SaasDeviceInfoMapper saasDeviceInfoMapper;

    @Autowired
    private SaasSpaceInfoMapper saasSpaceInfoMapper;
    public SpaceEnergyTotal getDayPowerDetail(SpaceEnergyRequest spaceEnergyRequest) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        spaceEnergyTotal.setSpaceId(spaceEnergyRequest.getSpaceId());
        SaasSpaceInfo spaceInfo = saasSpaceInfoMapper.selectById(spaceEnergyRequest.getSpaceId());
        if(spaceInfo==null){
            log.error("getDayPowerDetail spaceInfo is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return spaceEnergyTotal;
        }
        spaceEnergyTotal.setSpaceType(spaceInfo.getType());

        List<SpaceEnergyResponse> responseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponses = new ArrayList<>();
        Map<String, SpaceEnergyResponse> spaceEnergyMap = new LinkedHashMap<>();
        LambdaQueryWrapper<SaasSpaceDeviceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId());
        if(spaceInfo.getType().intValue() == SpaceTypeEnum.POWER_MONITORING.getCode()){
            queryWrapper.in(SaasSpaceDeviceRelation::getType, Constants.CT_IN, Constants.CT_OUT);
        }else if(spaceInfo.getType().intValue() == SpaceTypeEnum.SOCKET_MONITORING.getCode()){
            queryWrapper.eq(SaasSpaceDeviceRelation::getType, Constants.CT_SOCKET);
        }
        List<SaasSpaceDeviceRelation> list = saasSpaceDeviceRelationMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            Date recordDate = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd");
            long startTime = recordDate.getTime();
            for (SaasSpaceDeviceRelation saasSpaceDeviceRelation : list) {
                String tenantToken = rexolarApiClient.getTenantToken(saasSpaceDeviceRelation.getTenantId());
                DeviceBasicData deviceBasicData = rexolarApiClient.getDeviceByDeviceName(saasSpaceDeviceRelation.getDeviceName(), tenantToken);
                if(deviceBasicData==null){
                    log.error("getDayPowerDetail getDeviceByDeviceName error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                String deviceName = deviceBasicData.getName();
                deviceEnergyResponse.setDeviceName(deviceName);
                List<SaasDeviceInfo> deviceInfos = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>()
                        .eq(SaasDeviceInfo::getDeviceName, deviceName)
                );
                if(CollectionUtils.isEmpty(deviceInfos)){
                    deviceEnergyResponse.setDeviceAlias(deviceName);
                }else {
                    deviceEnergyResponse.setDeviceAlias(deviceInfos.get(0).getDeviceAlias());
                }

                long endTime = startTime + 86400000;
                if(DateUtil.isSameDay(recordDate, new Date())){
                    if(startTime < saasSpaceDeviceRelation.getGmtCreated().getTime()){
                        startTime  = saasSpaceDeviceRelation.getGmtCreated().getTime();
                    }
                }

                Map<String, Object> powerTmp = rexolarApiClient.getDeviceTimeSeries(
                        deviceBasicData.getCoreId(), tenantToken,
                        new String[]{Constants.TotalActivePower_1, Constants.ActivePower_1},
                        startTime, endTime,
                        100000, false, 300000L, "AVG", false
                );
                if(CollectionUtils.isEmpty(powerTmp)){
                    log.error("getDayPowerDetail getDeviceTimeSeries error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                List<Map> powerList = new ArrayList<>();
                if(spaceInfo.getType().intValue() == SpaceTypeEnum.POWER_MONITORING.getCode()){
                    powerList = (List<Map>) powerTmp.get(Constants.TotalActivePower_1);
                }else if(spaceInfo.getType().intValue() == SpaceTypeEnum.SOCKET_MONITORING.getCode()){
                    powerList = (List<Map>) powerTmp.get(Constants.ActivePower_1);
                }
                if(CollectionUtils.isEmpty(powerList)){
                    log.error("getDayPowerDetail getDeviceTimeSeries powerList is empty, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                }
                List<DeviceTimeSeriesData> powerDataList = new ArrayList<>();
                for(int i = 0; i < powerList.size(); i++){
                    DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData();
                    String ts = powerList.get(i).get("ts").toString();
                    Float value = Float.valueOf(powerList.get(i).get("value").toString());
                    deviceTimeSeriesData.setTs(ts);
                    deviceTimeSeriesData.setValue(value);
                    powerDataList.add(deviceTimeSeriesData);

                    SpaceEnergyResponse spaceEnergyResponse = spaceEnergyMap.get(ts);
                    if(spaceEnergyResponse == null){
                        spaceEnergyResponse = new SpaceEnergyResponse();
                        spaceEnergyResponse.setRecordDate(ts);
                        spaceEnergyResponse.setSpaceId(spaceEnergyRequest.getSpaceId());
                        if(saasSpaceDeviceRelation.getType()==Constants.CT_IN){
                            spaceEnergyResponse.setGPower( value);
                        }else if(saasSpaceDeviceRelation.getType()==Constants.CT_OUT){
                            spaceEnergyResponse.setPPower( value);
                        }else if (saasSpaceDeviceRelation.getType()==Constants.CT_SOCKET){
                            spaceEnergyResponse.setLPower( value);
                        }else {
                            log.error("getDayPowerDetail type error, saasSpaceDeviceRelation :{}", saasSpaceDeviceRelation);
                        }
                        spaceEnergyMap.put(ts, spaceEnergyResponse);
                    }else{
                        if(saasSpaceDeviceRelation.getType()==Constants.CT_IN){
                            spaceEnergyResponse.setGPower( spaceEnergyResponse.getGPower() + value);
                        }else if(saasSpaceDeviceRelation.getType()==Constants.CT_OUT){
                            spaceEnergyResponse.setPPower( spaceEnergyResponse.getGPower() + value);
                        }else if (saasSpaceDeviceRelation.getType()==Constants.CT_SOCKET){
                            spaceEnergyResponse.setLPower( spaceEnergyResponse.getGPower() + value);
                        }else {
                            log.error("getDayPowerDetail type error, saasSpaceDeviceRelation :{}", saasSpaceDeviceRelation);
                        }
                    }
                }
                deviceEnergyResponse.setPowerList(powerDataList);
                deviceEnergyResponses.add(deviceEnergyResponse);
                Map<String, Object> tmp = rexolarApiClient.getDeviceTimeSeries(
                        deviceBasicData.getCoreId(), tenantToken,
                        new String[]{Constants.TotalCEI_1, Constants.TotalCEE_1, Constants.CEI_1},
                        startTime, endTime,
                        100000, false, 300000L, "AVG", false
                );
                if(CollectionUtils.isEmpty(tmp)){
                    log.error("getDayPowerDetail getDeviceTimeSeries error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                List<Map> ceiList = (List<Map>) tmp.get(Constants.TotalCEI_1);
                if (saasSpaceDeviceRelation.getType().equals(Constants.CT_SOCKET)){
                    ceiList = (List<Map>) tmp.get(Constants.CEI_1);
                }
                if(!CollectionUtils.isEmpty(ceiList)){
                    Map start = ceiList.get(0);
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    log.info("getDayPowerDetail start cei is :{}", start);
                    List<DeviceTimeSeriesData> deviceCeiList = new ArrayList<>();

                    for(int i = 0; i < ceiList.size(); i++){
                        Map tmpCei = ceiList.get(i);
                        String ts = String.valueOf(tmpCei.get("ts"));
                        Float value = Float.parseFloat((String) tmpCei.get("value"));
                        Float tmpValue = value - startValue;
                        DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                        deviceCeiList.add(deviceTimeSeriesData);

                        SpaceEnergyResponse spaceEnergyResponse =  spaceEnergyMap.get(ts);
                        if(spaceEnergyResponse==null){
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(tmpValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeiTotal(tmpValue);
                            }
                            spaceEnergyMap.put(ts, spaceEnergyResponse);
                        }else{
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(spaceEnergyResponse.getPCeiTotal() + tmpValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeiTotal(spaceEnergyResponse.getGCeiTotal() + tmpValue);
                            }
                        }
                        startValue = value;
                    }

                    deviceEnergyResponse.setCeiList(deviceCeiList);
                    float ceiTotal = Float.parseFloat(ceiList.get(ceiList.size()-1).get("value").toString())
                            - Float.parseFloat(ceiList.get(0).get("value").toString());
                    deviceEnergyResponse.setCeiTotal(ceiTotal);
                }

                List<Map> ceeList = (List<Map>) tmp.get(Constants.TotalCEE_1);
                if(!CollectionUtils.isEmpty(ceeList)) {
                    Map start = ceeList.get(0);
                    log.info("getDayPowerDetail start TotalCEE_1 is :{}", start);
                    List<DeviceTimeSeriesData> deviceCeeList = new ArrayList<>();
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    for (int i = 0; i < ceeList.size(); i++) {
                        Map tmpCee = ceeList.get(i);
                        String ts = String.valueOf(tmpCee.get("ts"));
                        Float value = Float.parseFloat((String) tmpCee.get("value"));

                        Float tmpValue = value - startValue;
                        DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                        deviceCeeList.add(deviceTimeSeriesData);

                        SpaceEnergyResponse spaceEnergyResponse = spaceEnergyMap.get(ts);
                        if (spaceEnergyResponse == null) {
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(tmpValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeeTotal(tmpValue);
                            }
                            spaceEnergyMap.put(ts, spaceEnergyResponse);
                        } else {
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(spaceEnergyResponse.getPCeeTotal() + tmpValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeeTotal(spaceEnergyResponse.getGCeeTotal() + tmpValue);
                            }
                        }
                        startValue = value;
                    }
                    deviceEnergyResponse.setCeeList(deviceCeeList);
                    float ceeTotal = Float.parseFloat(ceeList.get(ceeList.size()-1).get("value").toString())
                            - Float.parseFloat(ceeList.get(0).get("value").toString());
                    deviceEnergyResponse.setCeeTotal(ceeTotal);
                }
                deviceEnergyResponses.add(deviceEnergyResponse);
            }

            float pCeeTotal = 0f;
            float pCeiTotal = 0f;
            float gCeiTotal = 0f;
            float gCeeTotal = 0f;
            for (Map.Entry<String, SpaceEnergyResponse> entry : spaceEnergyMap.entrySet()){
                SpaceEnergyResponse spaceEnergyResponse = entry.getValue();
                if(spaceEnergyResponse.getGCeeTotal()==0f && spaceEnergyResponse.getPCeeTotal()==0f
                        && spaceEnergyResponse.getGCeiTotal()==0f && spaceEnergyResponse.getPCeiTotal()==0f){
                    continue;
                }
                spaceEnergyResponse.setRecordDate(entry.getKey());
                spaceEnergyResponse.setSpaceId(spaceEnergyRequest.getSpaceId());
                spaceEnergyResponse.setPhotovoltaic(spaceEnergyResponse.getPCeiTotal());
                spaceEnergyResponse.setLoadElectricity(spaceEnergyResponse.getPCeiTotal() + spaceEnergyResponse.getGCeiTotal() - spaceEnergyResponse.getGCeeTotal());
                spaceEnergyResponse.setGridElectricity(spaceEnergyResponse.getGCeiTotal());

                if (spaceInfo.getType() == 1){
                    spaceEnergyResponse.setLPower(spaceEnergyResponse.getGPower() + spaceEnergyResponse.getPPower() - spaceEnergyResponse.getCPower());
                }

                responseList.add(spaceEnergyResponse);

                pCeeTotal += spaceEnergyResponse.getPCeeTotal();
                pCeiTotal += spaceEnergyResponse.getPCeiTotal();
                gCeiTotal += spaceEnergyResponse.getGCeiTotal();
                gCeeTotal += spaceEnergyResponse.getGCeeTotal();
            }
            spaceEnergyTotal.setPCeeTotal(pCeeTotal);
            spaceEnergyTotal.setPCeiTotal(pCeiTotal);
            spaceEnergyTotal.setGCeeTotal(gCeeTotal);
            spaceEnergyTotal.setGCeiTotal(gCeiTotal);
            spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
            spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
            spaceEnergyTotal.setLoadElectricityTotal(pCeiTotal + gCeiTotal - gCeeTotal);
            spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
            spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal-gCeeTotal);

            if(spaceInfo.getType().equals(SpaceTypeEnum.SOCKET_MONITORING.getCode())){
                JSONObject extendsInfo = JSONObject.parseObject(spaceInfo.getExtendsInfo());
                if(extendsInfo!=null && extendsInfo.containsKey("baseLoad")){
                    int hour = 24;
                    Float baseLoad = extendsInfo.getFloat("baseLoad");
                    LocalDate recordDate1 = LocalDate.parse(spaceEnergyRequest.getRecordDate());
                    LocalDate today = LocalDate.now();
                    if(recordDate1.equals( today)){
                        hour = LocalTime.now().getHour();
                    }
                    float baseLoadTotal = baseLoad * hour / 1000f;
//            spaceEnergyTotal.setSpaceCeiTotal(spaceEnergyTotal.getDeviceCeiTotal() + baseLoadTotal);
                    spaceEnergyTotal.setBaseLoadTotal(baseLoadTotal);
                    spaceEnergyTotal.setBaseLoadTotal(spaceEnergyTotal.getLoadElectricityTotal() + baseLoadTotal);
//            spaceEnergyTotal.setBaseLoadPercent(baseLoadTotal / spaceEnergyTotal.getSpaceCeiTotal() * 100);
//        }else {
//            spaceEnergyTotal.setSpaceCeiTotal(spaceEnergyTotal.getDeviceCeiTotal());
                }
            }


            float spaceCei = spaceEnergyTotal.getBaseLoadTotal() + spaceEnergyTotal.getGCeiTotal();
            for(DeviceEnergyResponse deviceEnergyResponse : deviceEnergyResponses){
                deviceEnergyResponse.setCeiPercent(deviceEnergyResponse.getCeiTotal() / spaceCei * 100);
            }

            spaceEnergyTotal.setSpaceEnergyResponseList(responseList);
            spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponses);
        }else {
            log.error("getDayPowerDetail saasSpaceDeviceRelation is empty, spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
        }
        sortSpaceEnergyTotal(spaceEnergyTotal);
//        spaceEnergyTotal.setSpaceEnergyResponseList(new ArrayList<>(spaceEnergyMap.values()));
        return spaceEnergyTotal;
    }

    private void sortSpaceEnergyTotal(SpaceEnergyTotal spaceEnergyTotal) {
        List<SpaceEnergyResponse> spaceEnergyList = spaceEnergyTotal.getSpaceEnergyResponseList();
        if (spaceEnergyList != null){
            spaceEnergyList.sort(new Comparator<SpaceEnergyResponse>() {
                @Override
                public int compare(SpaceEnergyResponse o1, SpaceEnergyResponse o2) {
                    return o1.getRecordDate().compareTo(o2.getRecordDate());
                }
            });
        }

        List<DeviceEnergyResponse> deviceEnergyList = spaceEnergyTotal.getDeviceEnergyResponseList();
        if (deviceEnergyList != null){
            for (DeviceEnergyResponse deviceEnergyResponse : deviceEnergyList){
                List<DeviceTimeSeriesData> ceiList = deviceEnergyResponse.getCeiList();
                if (ceiList != null){
                    ceiList.sort(new Comparator<DeviceTimeSeriesData>() {
                        @Override
                        public int compare(DeviceTimeSeriesData o1, DeviceTimeSeriesData o2) {
                            return o1.getTs().compareTo(o2.getTs());
                        }
                    });
                }
                List<DeviceTimeSeriesData> ceeList = deviceEnergyResponse.getCeeList();
                if (ceeList != null){
                    ceeList.sort(new Comparator<DeviceTimeSeriesData>() {
                        @Override
                        public int compare(DeviceTimeSeriesData o1, DeviceTimeSeriesData o2) {
                            return o1.getTs().compareTo(o2.getTs());
                        }
                    });
                }
                List<DeviceTimeSeriesData> powerList = deviceEnergyResponse.getPowerList();
                if (powerList != null){
                    powerList.sort(new Comparator<DeviceTimeSeriesData>() {
                        @Override
                        public int compare(DeviceTimeSeriesData o1, DeviceTimeSeriesData o2) {
                            return o1.getTs().compareTo(o2.getTs());
                        }
                    });
                }
            }
        }

    }

    public SpaceEnergyTotal getDayEnergyDetail(SpaceEnergyRequest spaceEnergyRequest) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        spaceEnergyTotal.setSpaceId(spaceEnergyRequest.getSpaceId());
        SaasSpaceInfo spaceInfo = saasSpaceInfoMapper.selectById(spaceEnergyRequest.getSpaceId());
        if(spaceInfo==null){
            log.error("getDayEnergyDetail spaceInfo is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return spaceEnergyTotal;
        }
        spaceEnergyTotal.setSpaceType(spaceInfo.getType());
        if(spaceInfo.getType().intValue() == SpaceTypeEnum.INVERTER_MONITORING.getCode()){
            return getInverterDayEnergyDetail(spaceEnergyRequest);
        }
        List<SpaceEnergyResponse> responseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponses = new ArrayList<>();
        Map<String, SpaceEnergyResponse> spaceEnergyMap = new LinkedHashMap<>();
        LambdaQueryWrapper<SaasSpaceDeviceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId());
        if(spaceInfo.getType().intValue() == SpaceTypeEnum.POWER_MONITORING.getCode()){
            queryWrapper.in(SaasSpaceDeviceRelation::getType, Constants.CT_IN, Constants.CT_OUT);
        }else if(spaceInfo.getType().intValue() == SpaceTypeEnum.SOCKET_MONITORING.getCode()){
            queryWrapper.eq(SaasSpaceDeviceRelation::getType, Constants.CT_SOCKET);
        }

        List<SaasSpaceDeviceRelation> list = saasSpaceDeviceRelationMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            Date recordDate = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd");
            long startTime = recordDate.getTime();
            for (SaasSpaceDeviceRelation saasSpaceDeviceRelation : list) {
                String tenantToken = rexolarApiClient.getTenantToken(saasSpaceDeviceRelation.getTenantId());
                DeviceBasicData deviceBasicData = rexolarApiClient.getDeviceByDeviceName(saasSpaceDeviceRelation.getDeviceName(), tenantToken);
                if(deviceBasicData==null){
                    log.error("getDayEnergyDetail getDeviceByDeviceName error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }

                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                String deviceName = deviceBasicData.getName();
                deviceEnergyResponse.setDeviceName(deviceName);
                List<SaasDeviceInfo> deviceInfos = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>()
                        .eq(SaasDeviceInfo::getDeviceName, deviceName)
                );
                if(CollectionUtils.isEmpty(deviceInfos)){
                    deviceEnergyResponse.setDeviceAlias(deviceName);
                }else {
                    deviceEnergyResponse.setDeviceAlias(deviceInfos.get(0).getDeviceAlias());
                }

                long endTime = startTime + 86400000;
                if(DateUtil.isSameDay(recordDate, new Date())){
                    if(startTime < saasSpaceDeviceRelation.getGmtCreated().getTime()){
                        startTime  = saasSpaceDeviceRelation.getGmtCreated().getTime();
                    }
                }

                Map<String, Object> tmp = rexolarApiClient.getDeviceTimeSeries(
                        deviceBasicData.getCoreId(), tenantToken,
                        new String[]{Constants.TotalCEI_1, Constants.TotalCEE_1, Constants.CEI_1},
                        startTime, endTime,
                        100000, false, 300000L, "AVG", false
                );
                if(CollectionUtils.isEmpty(tmp)){
                    log.error("getDayEnergyDetail getDeviceTimeSeries error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                List<Map> ceiList = (List<Map>) tmp.get(Constants.TotalCEI_1);
                if (saasSpaceDeviceRelation.getType().equals(Constants.CT_SOCKET)){
                    ceiList = (List<Map>) tmp.get(Constants.CEI_1);
                }
                if(!CollectionUtils.isEmpty(ceiList)){
                    Map start = ceiList.get(0);
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    log.info("getDayEnergyDetail start cei is :{}", start);
                    List<DeviceTimeSeriesData> deviceCeiList = new ArrayList<>();

                    for(int i = 0; i < ceiList.size(); i++){
                        Map tmpCei = ceiList.get(i);
                        String ts = String.valueOf(tmpCei.get("ts"));
                        Float value = Float.parseFloat((String) tmpCei.get("value"));
                        Float tmpValue = value - startValue;
                        DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                        deviceCeiList.add(deviceTimeSeriesData);

                        SpaceEnergyResponse spaceEnergyResponse =  spaceEnergyMap.get(ts);
                        if(spaceEnergyResponse==null){
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(tmpValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeiTotal(tmpValue);
                            }
                            spaceEnergyMap.put(ts, spaceEnergyResponse);
                        }else{
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(spaceEnergyResponse.getPCeiTotal() + tmpValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeiTotal(spaceEnergyResponse.getGCeiTotal() + tmpValue);
                            }
                        }
                        startValue = value;
                    }

                    deviceEnergyResponse.setCeiList(deviceCeiList);
                    float ceiTotal = Float.parseFloat(ceiList.get(ceiList.size()-1).get("value").toString())
                            - Float.parseFloat(ceiList.get(0).get("value").toString());
                    deviceEnergyResponse.setCeiTotal(ceiTotal);
                }

                List<Map> ceeList = (List<Map>) tmp.get(Constants.TotalCEE_1);
                if(!CollectionUtils.isEmpty(ceeList)) {
                    Map start = ceeList.get(0);
                    log.info("getDayEnergyDetail start TotalCEE_1 is :{}", start);
                    List<DeviceTimeSeriesData> deviceCeeList = new ArrayList<>();
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    for (int i = 0; i < ceeList.size(); i++) {
                        Map tmpCee = ceeList.get(i);
                        String ts = String.valueOf(tmpCee.get("ts"));
                        Float value = Float.parseFloat((String) tmpCee.get("value"));

                        Float tmpValue = value - startValue;
                        DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                        deviceCeeList.add(deviceTimeSeriesData);

                        SpaceEnergyResponse spaceEnergyResponse = spaceEnergyMap.get(ts);
                        if (spaceEnergyResponse == null) {
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(tmpValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeeTotal(tmpValue);
                            }
                            spaceEnergyMap.put(ts, spaceEnergyResponse);
                        } else {
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(spaceEnergyResponse.getPCeeTotal() + tmpValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN
                                    || saasSpaceDeviceRelation.getType() == Constants.CT_SOCKET){
                                spaceEnergyResponse.setGCeeTotal(spaceEnergyResponse.getGCeeTotal() + tmpValue);
                            }
                        }
                        startValue = value;
                    }
                    deviceEnergyResponse.setCeeList(deviceCeeList);
                    float ceeTotal = Float.parseFloat(ceeList.get(ceeList.size()-1).get("value").toString())
                            - Float.parseFloat(ceeList.get(0).get("value").toString());
                    deviceEnergyResponse.setCeeTotal(ceeTotal);
                }
                deviceEnergyResponses.add(deviceEnergyResponse);
            }
        }

        float pCeeTotal = 0f;
        float pCeiTotal = 0f;
        float gCeiTotal = 0f;
        float gCeeTotal = 0f;
        for (Map.Entry<String, SpaceEnergyResponse> entry : spaceEnergyMap.entrySet()){
            SpaceEnergyResponse spaceEnergyResponse = entry.getValue();
            if(spaceEnergyResponse.getGCeeTotal()==0f && spaceEnergyResponse.getPCeeTotal()==0f
                    && spaceEnergyResponse.getGCeiTotal()==0f && spaceEnergyResponse.getPCeiTotal()==0f){
                continue;
            }
            spaceEnergyResponse.setRecordDate(entry.getKey());
            spaceEnergyResponse.setSpaceId(spaceEnergyRequest.getSpaceId());
            spaceEnergyResponse.setPhotovoltaic(spaceEnergyResponse.getPCeiTotal());
            spaceEnergyResponse.setLoadElectricity(spaceEnergyResponse.getPCeiTotal() + spaceEnergyResponse.getGCeiTotal() - spaceEnergyResponse.getGCeeTotal());
            spaceEnergyResponse.setGridElectricity(spaceEnergyResponse.getGCeiTotal());

            responseList.add(spaceEnergyResponse);

            pCeeTotal += spaceEnergyResponse.getPCeeTotal();
            pCeiTotal += spaceEnergyResponse.getPCeiTotal();
            gCeiTotal += spaceEnergyResponse.getGCeiTotal();
            gCeeTotal += spaceEnergyResponse.getGCeeTotal();
        }
        spaceEnergyTotal.setPCeeTotal(pCeeTotal);
        spaceEnergyTotal.setPCeiTotal(pCeiTotal);
        spaceEnergyTotal.setGCeeTotal(gCeeTotal);
        spaceEnergyTotal.setGCeiTotal(gCeiTotal);
        spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
        spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
        spaceEnergyTotal.setLoadElectricityTotal(pCeiTotal + gCeiTotal - gCeeTotal);
        spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal-gCeeTotal);

        if(spaceInfo.getType().equals(SpaceTypeEnum.SOCKET_MONITORING.getCode())){
            JSONObject extendsInfo = JSONObject.parseObject(spaceInfo.getExtendsInfo());
            if(extendsInfo!=null && extendsInfo.containsKey("baseLoad")){
                int hour = 24;
                Float baseLoad = extendsInfo.getFloat("baseLoad");
                LocalDate recordDate = LocalDate.parse(spaceEnergyRequest.getRecordDate());
                LocalDate today = LocalDate.now();
                if(recordDate.equals( today)){
                    hour = LocalTime.now().getHour();
                }
                float baseLoadTotal = baseLoad * hour / 1000f;
//            spaceEnergyTotal.setSpaceCeiTotal(spaceEnergyTotal.getDeviceCeiTotal() + baseLoadTotal);
                spaceEnergyTotal.setBaseLoadTotal(baseLoadTotal);
                spaceEnergyTotal.setBaseLoadTotal(spaceEnergyTotal.getLoadElectricityTotal() + baseLoadTotal);
//            spaceEnergyTotal.setBaseLoadPercent(baseLoadTotal / spaceEnergyTotal.getSpaceCeiTotal() * 100);
//        }else {
//            spaceEnergyTotal.setSpaceCeiTotal(spaceEnergyTotal.getDeviceCeiTotal());
            }
        }


        float spaceCei = spaceEnergyTotal.getBaseLoadTotal() + spaceEnergyTotal.getGCeiTotal();
        for(DeviceEnergyResponse deviceEnergyResponse : deviceEnergyResponses){
            deviceEnergyResponse.setCeiPercent(deviceEnergyResponse.getCeiTotal() / spaceCei * 100);
        }
        spaceEnergyTotal.setSpaceEnergyResponseList(responseList);
        spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponses);
        return spaceEnergyTotal;
    }

    private SpaceEnergyTotal getInverterDayEnergyDetail(SpaceEnergyRequest spaceEnergyRequest) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        List<SpaceEnergyResponse> responseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponses = new ArrayList<>();
        float pCeeTotal = 0f;
        float pCeiTotal = 0f;
        float gCeiTotal = 0f;
        float gCeeTotal = 0f;
        LambdaQueryWrapper<SaasSpaceDeviceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId());
        List<SaasSpaceDeviceRelation> list = saasSpaceDeviceRelationMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            long startTime = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd").getTime();
            Map<String, Float> spaceEnergyMap = new HashMap<>();
            for (SaasSpaceDeviceRelation saasSpaceDeviceRelation : list) {
                String tenantToken = rexolarApiClient.getTenantToken(saasSpaceDeviceRelation.getTenantId());
                DeviceBasicData deviceBasicData = rexolarApiClient.getDeviceByDeviceName(saasSpaceDeviceRelation.getDeviceName(), tenantToken);
                if(deviceBasicData==null){
                    log.error("getInverterDayEnergyDetail getDeviceByDeviceName error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }

                long endTime = startTime + 86400000;
                if(startTime < saasSpaceDeviceRelation.getGmtCreated().getTime()){
                    startTime  = saasSpaceDeviceRelation.getGmtCreated().getTime();
                }
                if(saasSpaceDeviceRelation.getType() ==Constants.CT_WEINI){
                    Map<String, Object> tmp = rexolarApiClient.getDeviceTimeSeries(
                            deviceBasicData.getCoreId(), tenantToken,
                            new String[]{Constants.TotalCEE_1, Constants.TotalPV1_1, Constants.TotalPV2_1},
                            startTime, endTime,
                            100000, false, 300000L, "AVG", false
                    );
                    if(CollectionUtils.isEmpty(tmp)){
                        log.error("getInverterDayEnergyDetail getDeviceTimeSeries error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                        continue;
                    }
                    List<Map> ceeList = (List<Map>) tmp.get(Constants.TotalCEE_1);
                    if(!CollectionUtils.isEmpty(ceeList)){
                        Map start = ceeList.get(0);
                        Float startValue = Float.parseFloat((String) start.get("value"));
                        log.info("getInverterDayEnergyDetail start TotalCEE_1 is :{}", start);
                        List<DeviceTimeSeriesData> deviceCeeList = new ArrayList<>();
                        DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                        deviceEnergyResponse.setDeviceName(deviceBasicData.getName());
                        deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                        deviceEnergyResponse.setDeviceAlias(deviceBasicData.getLabel());

                        for(int i = 0; i < ceeList.size(); i++){
                            Map tmpCei = ceeList.get(i);
                            String ts = String.valueOf(tmpCei.get("ts"));
                            Float value = Float.parseFloat((String) tmpCei.get("value"));
                            Float tmpValue = value - startValue;
                            DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                            deviceCeeList.add(deviceTimeSeriesData);
                            startValue = value;
                            spaceEnergyMap.merge(ts, tmpValue, Float::sum);
                        }

                        deviceEnergyResponse.setCeeList(deviceCeeList);
                        float ceeTotal = Float.parseFloat(ceeList.get(ceeList.size()-1).get("value").toString())
                                - Float.parseFloat(ceeList.get(0).get("value").toString());
                        deviceEnergyResponse.setCeeTotal(ceeTotal);
                        deviceEnergyResponses.add(deviceEnergyResponse);

                    }

                    List<Map> pv1List = (List<Map>) tmp.get(Constants.TotalPV1_1);
                    if(!CollectionUtils.isEmpty(pv1List)) {
                        DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                        deviceEnergyResponse.setDeviceName(deviceBasicData.getName()+"_PV1");
                        deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                        deviceEnergyResponse.setDeviceAlias(deviceBasicData.getName()+"_PV1");
                        Map start = pv1List.get(0);
                        log.info("getInverterDayEnergyDetail start TotalPV1_1 is :{}", start);
                        List<DeviceTimeSeriesData> deviceCeeList = new ArrayList<>();
                        Float startValue = Float.parseFloat((String) start.get("value"));
                        for (int i = 0; i < pv1List.size(); i++) {
                            Map tmpCee = pv1List.get(i);
                            String ts = String.valueOf(tmpCee.get("ts"));
                            Float value = Float.parseFloat((String) tmpCee.get("value"));

                            Float tmpValue = value - startValue;
                            DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                            deviceCeeList.add(deviceTimeSeriesData);
                            startValue = value;
                        }
                        deviceEnergyResponse.setCeeList(deviceCeeList);
                        float ceeTotal = Float.parseFloat(ceeList.get(ceeList.size()-1).get("value").toString())
                                - Float.parseFloat(ceeList.get(0).get("value").toString());
                        deviceEnergyResponse.setCeeTotal(ceeTotal);
                        deviceEnergyResponses.add(deviceEnergyResponse);
                    }

                    List<Map> pv2List = (List<Map>) tmp.get(Constants.TotalPV2_1);
                    if(!CollectionUtils.isEmpty(pv2List)) {
                        DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                        deviceEnergyResponse.setDeviceName(deviceBasicData.getName()+"_PV2");
                        deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                        deviceEnergyResponse.setDeviceAlias(deviceBasicData.getName()+"_PV2");
                        Map start = pv1List.get(0);
                        log.info("getInverterDayEnergyDetail start TotalPV2_1 is :{}", start);
                        List<DeviceTimeSeriesData> deviceCeeList = new ArrayList<>();
                        Float startValue = Float.parseFloat((String) start.get("value"));
                        for (int i = 0; i < pv2List.size(); i++) {
                            Map tmpCee = pv2List.get(i);
                            String ts = String.valueOf(tmpCee.get("ts"));
                            Float value = Float.parseFloat((String) tmpCee.get("value"));

                            Float tmpValue = value - startValue;
                            DeviceTimeSeriesData deviceTimeSeriesData = new DeviceTimeSeriesData(ts, tmpValue);
                            deviceCeeList.add(deviceTimeSeriesData);
                            startValue = value;
                        }
                        deviceEnergyResponse.setCeeList(deviceCeeList);
                        float ceeTotal = Float.parseFloat(ceeList.get(pv2List.size()-1).get("value").toString())
                                - Float.parseFloat(pv2List.get(0).get("value").toString());
                        deviceEnergyResponse.setCeeTotal(ceeTotal);
                        deviceEnergyResponses.add(deviceEnergyResponse);
                    }
                }
                if(saasSpaceDeviceRelation.getType() ==Constants.CT_SOCKET){


                }

            }
            for (String ts : spaceEnergyMap.keySet()){
                SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
                spaceEnergyResponse.setRecordDate(ts);
                spaceEnergyResponse.setPhotovoltaic(spaceEnergyMap.get(ts));
                spaceEnergyResponse.setPCeeTotal(spaceEnergyMap.get(ts));
                responseList.add(spaceEnergyResponse);
                pCeeTotal += spaceEnergyMap.get(ts);
            }
        }

        spaceEnergyTotal.setPCeeTotal(pCeeTotal);
        spaceEnergyTotal.setPCeiTotal(pCeiTotal);
        spaceEnergyTotal.setGCeeTotal(gCeeTotal);
        spaceEnergyTotal.setGCeiTotal(gCeiTotal);
        spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
        spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
        spaceEnergyTotal.setLoadElectricityTotal(pCeiTotal + gCeiTotal - gCeeTotal);
        spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal-gCeeTotal);

        spaceEnergyTotal.setSpaceEnergyResponseList(responseList);
        spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponses);
        return spaceEnergyTotal;
    }

    public SpaceEnergyTotal getMonthEnergyDetail(SpaceEnergyRequest spaceEnergyRequest) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        spaceEnergyTotal.setSpaceId(spaceEnergyRequest.getSpaceId());
        SaasSpaceInfo spaceInfo = saasSpaceInfoMapper.selectById(spaceEnergyRequest.getSpaceId());
        if(spaceInfo==null){
            log.error("getMonthEnergyDetail spaceInfo is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return spaceEnergyTotal;
        }
        spaceEnergyTotal.setSpaceType(spaceInfo.getType());
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_DAY);
        queryWrapper.ge(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        queryWrapper.le(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-31");
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        float pCeeTotal = 0f;
        float pCeiTotal = 0f;
        float gCeiTotal = 0f;
        float gCeeTotal = 0f;
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponse.setGridElectricity(saasSpaceStatistics.getGCeiTotal());
            spaceEnergyResponse.setPhotovoltaic(saasSpaceStatistics.getPCeiTotal());
            spaceEnergyResponse.setLoadElectricity(saasSpaceStatistics.getGCeiTotal() + saasSpaceStatistics.getPCeiTotal() - saasSpaceStatistics.getGCeeTotal());

            spaceEnergyResponseList.add(spaceEnergyResponse);
            pCeeTotal += saasSpaceStatistics.getPCeeTotal();
            pCeiTotal += saasSpaceStatistics.getPCeiTotal();
            gCeeTotal += saasSpaceStatistics.getGCeeTotal();
            gCeiTotal += saasSpaceStatistics.getGCeiTotal();
        }
        spaceEnergyTotal.setSpaceEnergyResponseList(spaceEnergyResponseList);
        spaceEnergyTotal.setGCeiTotal(gCeiTotal);
        spaceEnergyTotal.setGCeeTotal(gCeeTotal);
        spaceEnergyTotal.setPCeiTotal(pCeiTotal);
        spaceEnergyTotal.setPCeeTotal(pCeeTotal);
        spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
        spaceEnergyTotal.setLoadElectricityTotal(gCeiTotal+pCeiTotal-gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
        spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal - gCeeTotal);

        LambdaQueryWrapper<SaasDeviceStatistics> deviceStatisticsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getRecordType, Constants.STATISTICS_DAY);
        deviceStatisticsLambdaQueryWrapper.ge(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        deviceStatisticsLambdaQueryWrapper.le(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-31");
        deviceStatisticsLambdaQueryWrapper.orderByAsc(SaasDeviceStatistics::getRecordDate);
        List<SaasDeviceStatistics> deviceStatistics = saasDeviceStatisticsMapper.selectList(deviceStatisticsLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(deviceStatistics)){
            Map<String, List<SaasDeviceStatistics>> deviceStatisticsMap = deviceStatistics.stream()
                    .collect(Collectors.groupingBy(SaasDeviceStatistics::getDeviceName));

            for(String deviceName : deviceStatisticsMap.keySet()){
                List<SaasDeviceStatistics> deviceStatisticsList = deviceStatisticsMap.get(deviceName);
                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setDeviceName(deviceName);
                List<SaasDeviceInfo> deviceInfos = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>()
                        .eq(SaasDeviceInfo::getDeviceName, deviceName)
                );
                if(CollectionUtils.isEmpty(deviceInfos)){
                    deviceEnergyResponse.setDeviceAlias(deviceName);
                }else {
                    deviceEnergyResponse.setDeviceAlias(deviceInfos.get(0).getDeviceAlias());
                }
                List<SaasSpaceDeviceRelation> relations = saasSpaceDeviceRelationMapper.selectList(new LambdaQueryWrapper<SaasSpaceDeviceRelation>()
                        .eq(SaasSpaceDeviceRelation::getDeviceName, deviceName).eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId()));
                if(!CollectionUtils.isEmpty(relations)){
                    deviceEnergyResponse.setType(relations.get(0).getType());
                }
                float totalCei = 0f;
                float totalCee = 0f;
                List<DeviceTimeSeriesData> ceeList = new ArrayList<>();
                List<DeviceTimeSeriesData> ceiList = new ArrayList<>();
                for(SaasDeviceStatistics saasDeviceStatistics : deviceStatisticsList){
                    totalCee += saasDeviceStatistics.getTotalCee();
                    totalCei += saasDeviceStatistics.getTotalCei();
                    ceeList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCee()));
                    ceiList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCei()));
                }
                deviceEnergyResponse.setCeeTotal(totalCee);
                deviceEnergyResponse.setCeiTotal(totalCei);
                deviceEnergyResponse.setCeeList(ceeList);
                deviceEnergyResponse.setCeiList(ceiList);
                deviceEnergyResponseList.add(deviceEnergyResponse);
            }
        }
        spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponseList);
        sortSpaceEnergyTotal(spaceEnergyTotal);
        return spaceEnergyTotal;
    }
    public SpaceEnergyTotal getYearEnergyDetail(SpaceEnergyRequest spaceEnergyRequest) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        spaceEnergyTotal.setSpaceId(spaceEnergyRequest.getSpaceId());
        SaasSpaceInfo spaceInfo = saasSpaceInfoMapper.selectById(spaceEnergyRequest.getSpaceId());
        if(spaceInfo==null){
            log.error("getYearEnergyDetail spaceInfo is null,spaceInfoId:{}", spaceEnergyRequest.getSpaceId());
            return spaceEnergyTotal;
        }
        spaceEnergyTotal.setSpaceType(spaceInfo.getType());
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_MONTH);
        queryWrapper.ge(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        queryWrapper.le(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-12");
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        float pCeeTotal = 0f;
        float pCeiTotal = 0f;
        float gCeiTotal = 0f;
        float gCeeTotal = 0f;
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponseList.add(spaceEnergyResponse);
            spaceEnergyResponse.setGridElectricity(saasSpaceStatistics.getGCeiTotal());
            spaceEnergyResponse.setPhotovoltaic(saasSpaceStatistics.getPCeiTotal());
            spaceEnergyResponse.setLoadElectricity(saasSpaceStatistics.getGCeiTotal() + saasSpaceStatistics.getPCeiTotal() - saasSpaceStatistics.getGCeeTotal());
            pCeeTotal += saasSpaceStatistics.getPCeeTotal();
            pCeiTotal += saasSpaceStatistics.getPCeiTotal();
            gCeeTotal += saasSpaceStatistics.getGCeeTotal();
            gCeiTotal += saasSpaceStatistics.getGCeiTotal();
        }
        spaceEnergyTotal.setSpaceEnergyResponseList(spaceEnergyResponseList);
        spaceEnergyTotal.setGCeiTotal(gCeiTotal);
        spaceEnergyTotal.setGCeeTotal(gCeeTotal);
        spaceEnergyTotal.setPCeiTotal(pCeiTotal);
        spaceEnergyTotal.setPCeeTotal(pCeeTotal);
        spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
        spaceEnergyTotal.setLoadElectricityTotal(gCeiTotal+pCeiTotal-gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
        spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal - gCeeTotal);

        LambdaQueryWrapper<SaasDeviceStatistics> deviceStatisticsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getRecordType, Constants.STATISTICS_MONTH);
        deviceStatisticsLambdaQueryWrapper.ge(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        deviceStatisticsLambdaQueryWrapper.le(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-12");
        deviceStatisticsLambdaQueryWrapper.orderByAsc(SaasDeviceStatistics::getRecordDate);
        List<SaasDeviceStatistics> deviceStatistics = saasDeviceStatisticsMapper.selectList(deviceStatisticsLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(deviceStatistics)) {
            Map<String, List<SaasDeviceStatistics>> deviceStatisticsMap = deviceStatistics.stream()
                    .collect(Collectors.groupingBy(SaasDeviceStatistics::getDeviceName));

            for (String deviceName : deviceStatisticsMap.keySet()) {
                List<SaasDeviceStatistics> deviceStatisticsList = deviceStatisticsMap.get(deviceName);
                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setDeviceName(deviceName);
                List<SaasDeviceInfo> deviceInfos = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>()
                        .eq(SaasDeviceInfo::getDeviceName, deviceName)
                );
                if (CollectionUtils.isEmpty(deviceInfos)) {
                    deviceEnergyResponse.setDeviceAlias(deviceName);
                } else {
                    deviceEnergyResponse.setDeviceAlias(deviceInfos.get(0).getDeviceAlias());
                }

                List<SaasSpaceDeviceRelation> relations = saasSpaceDeviceRelationMapper.selectList(new LambdaQueryWrapper<SaasSpaceDeviceRelation>()
                        .eq(SaasSpaceDeviceRelation::getDeviceName, deviceName).eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId()));
                if(!CollectionUtils.isEmpty(relations)){
                    deviceEnergyResponse.setType(relations.get(0).getType());
                }

                float totalCei = 0f;
                float totalCee = 0f;
                List<DeviceTimeSeriesData> ceeList = new ArrayList<>();
                List<DeviceTimeSeriesData> ceiList = new ArrayList<>();
                for(SaasDeviceStatistics saasDeviceStatistics : deviceStatisticsList){
                    totalCee += saasDeviceStatistics.getTotalCee();
                    totalCei += saasDeviceStatistics.getTotalCei();
                    ceeList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCee()));
                    ceiList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCei()));
                }
                deviceEnergyResponse.setCeeTotal(totalCee);
                deviceEnergyResponse.setCeiTotal(totalCei);
                deviceEnergyResponse.setCeeList(ceeList);
                deviceEnergyResponse.setCeiList(ceiList);
                deviceEnergyResponseList.add(deviceEnergyResponse);
            }
        }
        spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponseList);
        sortSpaceEnergyTotal(spaceEnergyTotal);
        return spaceEnergyTotal;
    }

    public SpaceEnergyTotal getAllEnergyDetail(Long spaceId) {
        SpaceEnergyTotal spaceEnergyTotal = new SpaceEnergyTotal();
        spaceEnergyTotal.setSpaceId(spaceId);
        SaasSpaceInfo spaceInfo = saasSpaceInfoMapper.selectById(spaceId);
        if(spaceInfo==null){
            log.error("getAllEnergyDetail spaceInfo is null,spaceInfoId:{}", spaceId);
            return spaceEnergyTotal;
        }
        spaceEnergyTotal.setSpaceType(spaceInfo.getType());
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        List<DeviceEnergyResponse> deviceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceId);
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_YEAR);
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        float pCeeTotal = 0f;
        float pCeiTotal = 0f;
        float gCeiTotal = 0f;
        float gCeeTotal = 0f;
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponse.setGridElectricity(saasSpaceStatistics.getGCeiTotal());
            spaceEnergyResponse.setPhotovoltaic(saasSpaceStatistics.getPCeiTotal());
            spaceEnergyResponse.setLoadElectricity(saasSpaceStatistics.getGCeiTotal() + saasSpaceStatistics.getPCeiTotal() - saasSpaceStatistics.getGCeeTotal());
            spaceEnergyResponseList.add(spaceEnergyResponse);
            pCeeTotal += saasSpaceStatistics.getPCeeTotal();
            pCeiTotal += saasSpaceStatistics.getPCeiTotal();
            gCeeTotal += saasSpaceStatistics.getGCeeTotal();
            gCeiTotal += saasSpaceStatistics.getGCeiTotal();
        }
        spaceEnergyTotal.setSpaceEnergyResponseList(spaceEnergyResponseList);
        spaceEnergyTotal.setGCeiTotal(gCeiTotal);
        spaceEnergyTotal.setGCeeTotal(gCeeTotal);
        spaceEnergyTotal.setPCeiTotal(pCeiTotal);
        spaceEnergyTotal.setPCeeTotal(pCeeTotal);
        spaceEnergyTotal.setGridElectricityTotal(gCeiTotal);
        spaceEnergyTotal.setLoadElectricityTotal(gCeiTotal+pCeiTotal-gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicTotal(pCeiTotal);
        spaceEnergyTotal.setSoldElectricityTotal(gCeeTotal);
        spaceEnergyTotal.setPhotovoltaicElectricityTotal(pCeiTotal - gCeeTotal);

        LambdaQueryWrapper<SaasDeviceStatistics> deviceStatisticsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getSpaceId, spaceId);
        deviceStatisticsLambdaQueryWrapper.eq(SaasDeviceStatistics::getRecordType, Constants.STATISTICS_YEAR);
        deviceStatisticsLambdaQueryWrapper.orderByAsc(SaasDeviceStatistics::getRecordDate);
        List<SaasDeviceStatistics> deviceStatistics = saasDeviceStatisticsMapper.selectList(deviceStatisticsLambdaQueryWrapper);
        if(!CollectionUtils.isEmpty(deviceStatistics)) {
            Map<String, List<SaasDeviceStatistics>> deviceStatisticsMap = deviceStatistics.stream()
                    .collect(Collectors.groupingBy(SaasDeviceStatistics::getDeviceName));

            for (String deviceName : deviceStatisticsMap.keySet()) {
                List<SaasDeviceStatistics> deviceStatisticsList = deviceStatisticsMap.get(deviceName);
                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setDeviceName(deviceName);
                List<SaasDeviceInfo> deviceInfos = saasDeviceInfoMapper.selectList(new LambdaQueryWrapper<SaasDeviceInfo>()
                        .eq(SaasDeviceInfo::getDeviceName, deviceName)
                );
                if (CollectionUtils.isEmpty(deviceInfos)) {
                    deviceEnergyResponse.setDeviceAlias(deviceName);
                } else {
                    deviceEnergyResponse.setDeviceAlias(deviceInfos.get(0).getDeviceAlias());
                }

                List<SaasSpaceDeviceRelation> relations = saasSpaceDeviceRelationMapper.selectList(new LambdaQueryWrapper<SaasSpaceDeviceRelation>()
                        .eq(SaasSpaceDeviceRelation::getDeviceName, deviceName).eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceId));
                if(!CollectionUtils.isEmpty(relations)){
                    deviceEnergyResponse.setType(relations.get(0).getType());
                }


                float totalCei = 0f;
                float totalCee = 0f;
                List<DeviceTimeSeriesData> ceeList = new ArrayList<>();
                List<DeviceTimeSeriesData> ceiList = new ArrayList<>();
                for(SaasDeviceStatistics saasDeviceStatistics : deviceStatisticsList){
                    totalCee += saasDeviceStatistics.getTotalCee();
                    totalCei += saasDeviceStatistics.getTotalCei();
                    ceeList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCee()));
                    ceiList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), saasDeviceStatistics.getTotalCei()));
                }
                deviceEnergyResponse.setCeeTotal(totalCee);
                deviceEnergyResponse.setCeiTotal(totalCei);
                deviceEnergyResponse.setCeeList(ceeList);
                deviceEnergyResponse.setCeiList(ceiList);
                deviceEnergyResponseList.add(deviceEnergyResponse);
            }
        }
        spaceEnergyTotal.setDeviceEnergyResponseList(deviceEnergyResponseList);
        sortSpaceEnergyTotal(spaceEnergyTotal);
        return spaceEnergyTotal;
    }

    public List<SpaceEnergyResponse> getDayEnergy(SpaceEnergyRequest spaceEnergyRequest) {
        List<SpaceEnergyResponse> responseList = new ArrayList<>();
        Map<String, SpaceEnergyResponse> map = new LinkedHashMap<>();
        LambdaQueryWrapper<SaasSpaceDeviceRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceDeviceRelation::getSpaceInfoId, spaceEnergyRequest.getSpaceId());
        List<SaasSpaceDeviceRelation> list = saasSpaceDeviceRelationMapper.selectList(queryWrapper);
        if(!CollectionUtils.isEmpty(list)){
            long startTime = DateUtil.parse(spaceEnergyRequest.getRecordDate(), "yyyy-MM-dd").getTime();
            for (SaasSpaceDeviceRelation saasSpaceDeviceRelation : list) {
                String tenantToken = rexolarApiClient.getTenantToken(saasSpaceDeviceRelation.getTenantId());
                DeviceBasicData deviceBasicData = rexolarApiClient.getDeviceByDeviceName(saasSpaceDeviceRelation.getDeviceName(), tenantToken);
                if(deviceBasicData==null){
                    log.error("getDayEnergy getDeviceByDeviceName error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                long endTime = startTime + 86400000;
                if(startTime < saasSpaceDeviceRelation.getGmtCreated().getTime()){
                    startTime  = saasSpaceDeviceRelation.getGmtCreated().getTime();
                }
                DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
                deviceEnergyResponse.setDeviceName(deviceBasicData.getName());
                deviceEnergyResponse.setType(saasSpaceDeviceRelation.getType());
                Map<String, Object> tmp = rexolarApiClient.getDeviceTimeSeries(
                        deviceBasicData.getCoreId(), tenantToken,
                        new String[]{Constants.TotalCEI_1, Constants.TotalCEE_1},
                        startTime, endTime,
                        100000, false, 300000L, "AVG", false
                        );
                if(CollectionUtils.isEmpty(tmp)){
                    log.error("getDayEnergy getDeviceTimeSeries error, saasSpaceDeviceRelation.deviceName:{}", saasSpaceDeviceRelation.getDeviceName());
                    continue;
                }
                List<DeviceTimeSeries> ceiList = (List<DeviceTimeSeries>) tmp.get(Constants.TotalCEI_1);
                if(!CollectionUtils.isEmpty(ceiList)){
                    Map start = ceiList.get(0);
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    log.info("getDayEnergy start TotalCEI_1 is :{}", start);
                    for(int i = 0; i < ceiList.size(); i++){
                        Map tmpCei = ceiList.get(i);
                        String ts = String.valueOf(tmpCei.get("ts"));
                        Float tmpValue = Float.parseFloat((String) tmpCei.get("value"));
                        SpaceEnergyResponse spaceEnergyResponse =  map.get(ts);
                        if(spaceEnergyResponse==null){
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(tmpValue - startValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN){
                                spaceEnergyResponse.setGCeiTotal(tmpValue - startValue);
                            }
                            map.put(ts, spaceEnergyResponse);
                        }else{
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT){
                                spaceEnergyResponse.setPCeiTotal(spaceEnergyResponse.getGCeiTotal() + tmpValue - startValue);
                            }else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN){
                                spaceEnergyResponse.setGCeiTotal(spaceEnergyResponse.getPCeiTotal() + tmpValue - startValue);
                            }
                        }
                        startValue = tmpValue;
                    }
                }

                List<DeviceTimeSeries> ceeList = (List<DeviceTimeSeries>) tmp.get(Constants.TotalCEE_1);
                if(!CollectionUtils.isEmpty(ceeList)) {
                    Map start = ceeList.get(0);
                    log.info("getDayEnergy start TotalCEE_1 is :{}", start);
                    Float startValue = Float.parseFloat((String) start.get("value"));
                    for (int i = 0; i < ceeList.size(); i++) {
                        Map tmpCee = ceeList.get(i);
                        String ts = String.valueOf(tmpCee.get("ts"));
                        Float tmpValue = Float.parseFloat((String) tmpCee.get("value"));
                        SpaceEnergyResponse spaceEnergyResponse = map.get(ts);
                        if (spaceEnergyResponse == null) {
                            spaceEnergyResponse = new SpaceEnergyResponse();
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(tmpValue - startValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN) {
                                spaceEnergyResponse.setGCeeTotal(tmpValue - startValue);
                            }
                            map.put(ts, spaceEnergyResponse);
                        } else {
                            if (saasSpaceDeviceRelation.getType() == Constants.CT_OUT) {
                                spaceEnergyResponse.setPCeeTotal(spaceEnergyResponse.getGCeeTotal() + tmpValue - startValue);
                            } else if (saasSpaceDeviceRelation.getType() == Constants.CT_IN) {
                                spaceEnergyResponse.setGCeeTotal(spaceEnergyResponse.getPCeeTotal() + tmpValue - startValue);
                            }
                        }
                        startValue = tmpValue;
                    }
                }
            }
        }

        for (Map.Entry<String, SpaceEnergyResponse> entry : map.entrySet()){
            SpaceEnergyResponse spaceEnergyResponse = entry.getValue();
            spaceEnergyResponse.setRecordDate(entry.getKey());
            spaceEnergyResponse.setSpaceId(spaceEnergyRequest.getSpaceId());
            responseList.add(spaceEnergyResponse);
        }
        return responseList;
    }

//
//    public SpaceEnergyResponse getDeviceMonthEnergy(SpaceEnergyRequest spaceEnergyRequest) {
//        SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
//        spaceEnergyResponse.setSpaceInfoId(spaceEnergyRequest.getSpaceInfoId());
//        LambdaQueryWrapper<SaasDeviceStatistics> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(SaasDeviceStatistics::getSpaceInfoId, spaceEnergyRequest.getSpaceInfoId());
//        queryWrapper.eq(SaasDeviceStatistics::getRecordType, Constants.STATISTICS_DAY);
//        queryWrapper.ge(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
//        queryWrapper.le(SaasDeviceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-31");
//        if(spaceEnergyRequest.getType()!=null){
//            queryWrapper.eq(SaasDeviceStatistics::getType, spaceEnergyRequest.getType());
//        }
//        List<SaasDeviceStatistics> list = saasDeviceStatisticsMapper.selectList(queryWrapper);
//        if(!CollectionUtils.isEmpty(list)){
//            getDeviceEnergyResponse(list, spaceEnergyResponse);
//        }
//        return spaceEnergyResponse;
//    }

//    private void getDeviceEnergyResponse(List<SaasDeviceStatistics> list, SpaceDeviceEnergyResponse spaceDeviceEnergyResponse) {
//        float gCeeTotal = 0f;
//        float gCeiTotal = 0f;
//        float pCeeTotal = 0f;
//        float pCeiTotal = 0f;
//        Map<String, List<SaasDeviceStatistics>> dataMap = list.stream().collect(Collectors.groupingBy(SaasDeviceStatistics::getGwName));
//        List<DeviceEnergyResponse> deviceEnergyResponseList = new ArrayList<>();
//        for (Map.Entry<String, List<SaasDeviceStatistics>> entry : dataMap.entrySet()){
//            List<SaasDeviceStatistics> deviceStatistics = entry.getValue();
//            deviceStatistics.sort((o1, o2) -> o1.getRecordDate().compareTo(o2.getRecordDate()));
//            DeviceEnergyResponse deviceEnergyResponse = new DeviceEnergyResponse();
//            deviceEnergyResponse.setGwName(entry.getKey());
//            deviceEnergyResponse.setType(deviceStatistics.get(0).getType());
//            List<DeviceTimeSeriesData> ceiList = new ArrayList<>();
//            List<DeviceTimeSeriesData> ceeList = new ArrayList<>();
//            Float totalCee = 0f;
//            Float totalCei = 0f;
//            for (SaasDeviceStatistics saasDeviceStatistics : deviceStatistics){
//                ceiList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), String.valueOf(saasDeviceStatistics.getTotalCei())));
//                totalCei += saasDeviceStatistics.getTotalCei();
//                ceeList.add(new DeviceTimeSeriesData(saasDeviceStatistics.getRecordDate(), String.valueOf(saasDeviceStatistics.getTotalCee())));
//                totalCee += saasDeviceStatistics.getTotalCee();
//            }
//            deviceEnergyResponse.setCeiList(ceiList);
//            deviceEnergyResponse.setCeeList(ceeList);
//            deviceEnergyResponse.setCeeTotal(totalCee);
//            deviceEnergyResponse.setCeiTotal(totalCei);
//            deviceEnergyResponseList.add(deviceEnergyResponse);
//            if(deviceStatistics.get(0).getType()==Constants.CT_IN){
//                gCeeTotal += totalCee;
//                gCeiTotal += totalCei;
//            }else if(deviceStatistics.get(0).getType()==Constants.CT_OUT){
//                pCeeTotal += totalCee;
//                pCeiTotal += totalCei;
//            }else {
//                log.error("设备类型错误, type =:{}", deviceStatistics.get(0).getType());
//            }
//
//        }
//        spaceDeviceEnergyResponse.setDeviceEnergyResponseList(deviceEnergyResponseList);
//        spaceDeviceEnergyResponse.setCeiTotal(gCeiTotal);
//        spaceDeviceEnergyResponse.setGCeeTotal(gCeeTotal);
//        spaceDeviceEnergyResponse.setPCeeTotal(pCeeTotal);
//        spaceDeviceEnergyResponse.setPCeiTotal(pCeiTotal);
//    }
    public List<SpaceEnergyResponse> getMonthEnergy(SpaceEnergyRequest spaceEnergyRequest) {
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_DAY);
        queryWrapper.ge(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        queryWrapper.le(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-31");
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponseList.add(spaceEnergyResponse);
        }
        return spaceEnergyResponseList;
    }
    public List<SpaceEnergyResponse> getYearEnergy(SpaceEnergyRequest spaceEnergyRequest) {
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceEnergyRequest.getSpaceId());
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_MONTH);
        queryWrapper.ge(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-01");
        queryWrapper.le(SaasSpaceStatistics::getRecordDate, spaceEnergyRequest.getRecordDate() + "-12");
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponseList.add(spaceEnergyResponse);
        }
        return spaceEnergyResponseList;
    }

    public List<SpaceEnergyResponse> getAllEnergy(Long spaceId) {
        List<SpaceEnergyResponse> spaceEnergyResponseList = new ArrayList<>();
        LambdaQueryWrapper<SaasSpaceStatistics> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SaasSpaceStatistics::getSpaceId, spaceId);
        queryWrapper.eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_YEAR);
        queryWrapper.orderByAsc(SaasSpaceStatistics::getRecordDate);
        List<SaasSpaceStatistics> list = saasSpaceStatisticsMapper.selectList(queryWrapper);
        for (SaasSpaceStatistics saasSpaceStatistics : list){
            SpaceEnergyResponse spaceEnergyResponse = new SpaceEnergyResponse();
            BeanUtils.copyProperties(saasSpaceStatistics, spaceEnergyResponse);
            spaceEnergyResponseList.add(spaceEnergyResponse);
        }
        return spaceEnergyResponseList;
    }

    public SpaceInComeResponse getYearInCome(SpaceInComeRequest spaceInComeRequest) {
        SpaceInComeResponse spaceInComeResponse = new SpaceInComeResponse();
        spaceInComeResponse.setSpaceId(spaceInComeRequest.getSpaceId());
        spaceInComeResponse.setYear(spaceInComeRequest.getYear());
        List<SpaceIncomeDetail> details = new ArrayList<>();
        List<SaasSpaceStatistics> datas = saasSpaceStatisticsMapper.selectList(new LambdaQueryWrapper<SaasSpaceStatistics>()
                .eq(SaasSpaceStatistics::getSpaceId, spaceInComeRequest.getSpaceId())
                .eq(SaasSpaceStatistics::getRecordType, Constants.STATISTICS_MONTH)
                .ge(SaasSpaceStatistics::getRecordDate, spaceInComeRequest.getYear() + "-01")
                .le(SaasSpaceStatistics::getRecordDate, spaceInComeRequest.getYear() + "-12")
                .orderByAsc(SaasSpaceStatistics::getRecordDate));
        if(CollectionUtils.isEmpty(datas)){
            log.error("getYearInCome saasSpaceStatisticsMapper data is empty, SpaceInComeRequest:{}", spaceInComeRequest);
            return spaceInComeResponse;
        }
        for(SaasSpaceStatistics saasSpaceStatistics : datas){
            String recordDate = saasSpaceStatistics.getRecordDate();
            SpaceIncomeDetail spaceIncomeDetail = new SpaceIncomeDetail();
            spaceIncomeDetail.setPhotovoltaic(saasSpaceStatistics.getPCeiTotal());
            spaceIncomeDetail.setGridElectricity(saasSpaceStatistics.getGCeiTotal());
            spaceIncomeDetail.setLoadElectricity(saasSpaceStatistics.getPCeiTotal() + saasSpaceStatistics.getGCeiTotal() - saasSpaceStatistics.getGCeeTotal());
            if (spaceIncomeDetail.getLoadElectricity()!=0.0f){
                spaceIncomeDetail.setPhotovoltaicPercent(spaceIncomeDetail.getPhotovoltaic() / spaceIncomeDetail.getLoadElectricity() * 100f);
                spaceIncomeDetail.setGridElectricityPercent(spaceIncomeDetail.getGridElectricity() / spaceIncomeDetail.getLoadElectricity() * 100f);
                spaceIncomeDetail.setBatteryPercent(spaceIncomeDetail.getBatteryPercent() / spaceIncomeDetail.getLoadElectricity() * 100f);
            }
            spaceIncomeDetail.setCo2(saasSpaceStatistics.getPCeiTotal() * 0.4f);
            spaceIncomeDetail.setBatteryPercent(0f);
            spaceIncomeDetail.setMonth(recordDate);
            details.add(spaceIncomeDetail);
        }
        spaceInComeResponse.setDetails(details);
        return spaceInComeResponse;
    }

    public Object getDayPower(Long spaceId, String recordDate, String tenantToken) {
        return null;
    }
}
